using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class CountsPerPeriod : Form
    {
        public CountsPerPeriod()
        {
            InitializeComponent();
        }

        private EventScavengerDAL sqlEventSource;
        private List<MachineLogEntry> logs = new List<MachineLogEntry>();
        private bool allLogs = true;
        private List<int> eventIDs = new List<int>();
        private bool allEventIDs = true;
        private List<string> sources = new List<string>();
        private bool allSources = true;

        public void ShowCountsPerPeriod(EventScavengerDAL sqlEventSource)
        {
            this.sqlEventSource = sqlEventSource;
            ShowDialog();
        }

        private void StatsCounts_Load(object sender, EventArgs e)
        {
            cboType.SelectedIndex = 0;
            cboPeriod.SelectedIndex = 0;
            dateTimeChooserFrom.SelectedDateTime = DateTime.Now.AddHours(-1);
            dateTimeChooserTo.SelectedDateTime = DateTime.Now;
        }

        private void cmdRun_Click(object sender, EventArgs e)
        {
            List<PeriodCount> periodCounts;
            try
            {
                if (chkHistoric.Checked)
                    periodCounts = sqlEventSource.GetCountsPerPeiodHistoric((CountsPerPeriodType)cboPeriod.SelectedIndex, (EventLogEntryTypes)cboType.SelectedIndex,
                        dateTimeChooserFrom.SelectedDateTime, dateTimeChooserTo.SelectedDateTime, logs, eventIDs, sources
                        );
                else
                    periodCounts = sqlEventSource.GetCountsPerPeiod((CountsPerPeriodType)cboPeriod.SelectedIndex, (EventLogEntryTypes)cboType.SelectedIndex,
                        dateTimeChooserFrom.SelectedDateTime, dateTimeChooserTo.SelectedDateTime, logs, eventIDs, sources
                        );
                lvwPeriodCounts.Items.Clear();
                foreach (PeriodCount p in periodCounts)
                {
                    ListViewItem lvi = new ListViewItem(p.PeriodStart.ToString("yyyy-MM-dd HH:mm"));
                    lvi.SubItems.Add(p.Count.ToString());
                    lvwPeriodCounts.Items.Add(lvi);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cmdLogsFilter_Click(object sender, EventArgs e)
        {
            MachineLogFilter machineLogFilter = new MachineLogFilter();
            machineLogFilter.AllEnabled = allLogs;
            machineLogFilter.SelectedMachineLogs = logs;
            if (machineLogFilter.Show(sqlEventSource) == DialogResult.OK)
            {
                allLogs  = machineLogFilter.AllEnabled;
                txtLogs.Text = "";
                if (!allLogs)
                {
                    logs = machineLogFilter.SelectedMachineLogs;
                    foreach (MachineLogEntry mle in logs)
                    {
                        txtLogs.Text += mle.Machine + "\\" + mle.LogName + ",";
                    }
                    txtLogs.Text = txtLogs.Text.Trim(',');
                }
                else
                    logs = new List<MachineLogEntry>();
            }
        }

        private void cmdEventIDFilter_Click(object sender, EventArgs e)
        {
            EventIDFilter eventIDFilter = new EventIDFilter();
            eventIDFilter.AllEnabled = allEventIDs;
            eventIDFilter.SelectedIds = eventIDs;
            if (eventIDFilter.Show(sqlEventSource) == DialogResult.OK)
            {
                allEventIDs = eventIDFilter.AllEnabled;
                txtEventIDs.Text = "";
                if (!allEventIDs)
                {
                    eventIDs = eventIDFilter.SelectedIds;
                    foreach (int eventID in eventIDs)
                    {
                        txtEventIDs.Text += eventID.ToString() + ",";
                    }
                    txtEventIDs.Text = txtEventIDs.Text.Trim(',');
                }
                else
                    eventIDs = new List<int>();
            }
        }

        private void cmdSourceFilter_Click(object sender, EventArgs e)
        {
            EventSourceFilter eventSourceFilter = new EventSourceFilter();
            eventSourceFilter.AllEnabled = allSources;
            eventSourceFilter.SelectedSources = sources;
            if (eventSourceFilter.Show(sqlEventSource) == DialogResult.OK)
            {
                allSources = eventSourceFilter.AllEnabled;
                txtSources.Text = "";
                if (!allSources)
                {
                    sources = eventSourceFilter.SelectedSources;
                    foreach (string source in sources)
                    {
                        txtSources.Text += source + ",";
                    }
                    txtSources.Text = txtSources.Text.Trim(',');
                }
                else
                    sources = new List<string>();
            }
        }

        private void exportToCsvToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lvwPeriodCounts.Items.Count > 0)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (ListViewItem lvi in lvwPeriodCounts.Items)
                    {
                        sb.AppendLine(lvi.Text + "," + lvi.SubItems[1].Text);
                    }
                    System.IO.File.WriteAllText(saveFileDialog1.FileName, sb.ToString());
                }
            }
        }
    }
}